package com.caizi.file;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.caizi.util.MD5Util;

public class DirCompare {

    private File fileBosent = new File("F:/workspace/bosent_baseline_application_agent2.2/bosentbusiness");

    private File fileParse = new File("F:/workspace/file_parse_module/deploy");

    private List<String> fileNames = new ArrayList<String>(); 

    private void findAgentXml(File file) throws IOException {
        File[] files = file.listFiles(new FileFilter() {
            public boolean accept(File file) {
                if (file.getName().endsWith(".svn")) {
                    return false;
                }
                if (file.isDirectory() || (file.getName().startsWith("MB_") && file.getName().endsWith(".xml"))) {
                    return true;
                }
                return false;
            }
        });
        for (File thisFile : files) {
            if (thisFile.isFile()) {
                if (fileNames.contains(thisFile.getName())) {
                    System.out.println("平台中文件[" + thisFile.getAbsolutePath() + "]重复了!!!!!!!!!");
                    continue;
                }
                fileNames.add(thisFile.getName());
                String md5bosent = MD5Util.getFileMD5String(thisFile);
                List<File> parseXmls = findParseXml(fileParse, thisFile.getName());
                if (null == parseXmls || parseXmls.size() == 0) {
                    System.out.print("File bosent Xml [" + thisFile.getAbsolutePath() + "], ");
                    System.out.println("Not find parse Xml!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
                    continue;
                }
                File parseXml = parseXmls.get(0);
                if (parseXmls.size() > 1) {
                    System.out.println("文件解析中文件[" + parseXml.getAbsolutePath() + "]重复了!!!!!!!!!");
                    continue;
                }
                String md5parse = MD5Util.getFileMD5String(parseXmls.get(0));

                if (!md5bosent.equalsIgnoreCase(md5parse)) {
                    System.out.print("File bosent Xml [" + thisFile.getAbsolutePath() + "], ");
                    System.out.print("File parse Xml [" + parseXml.getAbsolutePath() + "], ");
                    System.out.println("two Xmls is different!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
                }
                moveXml(parseXml);
            } else if (thisFile.isDirectory()) {
                findAgentXml(thisFile);
            } else {
                // Do nothing
            }
        }
    }

    private void moreParseJar(File file) {
        File[] files = file.listFiles(new FileFilter() {
            public boolean accept(File file) {
                if (file.getName().endsWith(".svn")) {
                    return false;
                }
                if (file.isDirectory() || (file.getName().startsWith("MB_") && file.getName().endsWith(".xml"))) {
                    return true;
                }
                return false;
            }
        });
        for (File thisFile : files) {
            if (thisFile.isFile()) {
                System.out.print("Not find bosent Xml!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
                System.out.println("File parse Xml [" + thisFile.getAbsolutePath() + "]");
            } else {
                moreParseJar(thisFile);
            }
        }
    }

    private List<File> findParseXml(File file, final String name) {
        List<File> xmls = new ArrayList<File>();
        File[] files = file.listFiles(new FileFilter() {
            public boolean accept(File file) {
                if (file.getName().endsWith(".svn")) {
                    return false;
                }
                if (file.isDirectory() || (file.getName().equals(name))) {
                    return true;
                }
                return false;
            }
        });
        for (File thisFile : files) {
            if (thisFile.isDirectory()) {
                xmls.addAll(findParseXml(thisFile, name));
            } else {
                xmls.add(thisFile);
                return xmls;
            }
        }
        return xmls;
    }

    private void moveXml(File xml) {
        File newDir = new File(fileParse.getParent() + File.separator + "newdir");
        if (!newDir.exists()) {
            boolean isok = newDir.mkdir();
            if (!isok) {
                System.out.println("[[[[[[[[[[[[[[[[[[[ERROR]]]]]]]]]]]]]]]]]]]");
            }
        }
        File newFile = new File(newDir.getAbsolutePath() + File.separator + xml.getName());
        xml.renameTo(newFile);
    }

    public static void main(String... strings) throws IOException {
        DirCompare fc = new DirCompare();
        fc.findAgentXml(fc.fileBosent);
        fc.moreParseJar(fc.fileParse);
    }
}
